name: coverage

defaults:
  #if: "!contains(github.event.head_commit.message, 'skip ci')"  # SKIP
  run:
    # Use a bash shell so we can use the same syntax for environment variable
    # access regardless of the host operating system
    shell: bash -e -x {0}

on:
  # https://github.community/t/how-to-trigger-an-action-on-push-or-pull-request-but-not-both/16662
  workflow_dispatch:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master

env:
  PROJ_PFX_TARGET: c4core-
  PROJ_PFX_CMAKE: C4CORE_
  CMAKE_FLAGS:
  NUM_JOBS_BUILD: # 4


jobs:

  #----------------------------------------------------------------------------
  coverage:
    name: coverage/${{matrix.name}}
    # if: github.ref == 'refs/heads/master'
    continue-on-error: true
    if: always()  # https://stackoverflow.com/questions/62045967/github-actions-is-there-a-way-to-continue-on-error-while-still-getting-correct
    runs-on: ${{matrix.os}}
    strategy:
      fail-fast: false
      matrix:
        include:
          - {name: c++11, std: 11, cxx: g++-9, cc: gcc-9, bt: Coverage, os: ubuntu-20.04}
          - {name: c++17, std: 17, cxx: g++-9, cc: gcc-9, bt: Coverage, os: ubuntu-20.04}
          #- {name: c++20, std: 20, cxx: g++-9, cc: gcc-9, bt: Coverage, os: ubuntu-20.04}
    env: {
      STD: "${{matrix.std}}", CXX_: "${{matrix.cxx}}", BT: "${{matrix.bt}}", BITLINKS: "${{matrix.bitlinks}}", VG: "${{matrix.vg}}", SAN: "${{matrix.san}}", LINT: "${{matrix.lint}}", OS: "${{matrix.os}}",
      CODECOV_TOKEN: "${{secrets.CODECOV_TOKEN}}",
      COVERALLS_REPO_TOKEN: "${{secrets.COVERALLS_REPO_TOKEN}}",
      COVERALLS_PARALLEL: true,
      }
    steps:
      - {name: checkout, uses: actions/checkout@v3, with: {submodules: recursive}}
      - {name: install requirements, run: source .github/reqs.sh && c4_install_test_requirements $OS}
      - {name: show info, run: source .github/setenv.sh && c4_show_info}
      - name: static64-configure---------------------------------------------------
        run: source .github/setenv.sh && c4_cfg_test static64
      - {name: static64-build, run: source .github/setenv.sh && c4_build_test static64}
      - {name: static64-run, run: source .github/setenv.sh && c4_build_target static64 c4core-coverage}
      - name: static64-coverage-artifacts
        uses: actions/upload-artifact@v3
        with:
          name: coverage-static64
          path: |
            build/static64/lcov/
            build/static64/coverage3-final_filtered.lcov
      - {name: static64-submit-codecov, run: source .github/setenv.sh && c4_submit_coverage static64 codecov}
      - {name: static64-submit-coveralls, run: source .github/setenv.sh && c4_submit_coverage static64 coveralls,
         env: {COVERALLS_FLAG_NAME: "${{matrix.name}}/static64"}}
      - name: static32-configure---------------------------------------------------
        run: source .github/setenv.sh && c4_cfg_test static32
      - {name: static32-build, run: source .github/setenv.sh && c4_build_test static32}
      - {name: static32-run, run: source .github/setenv.sh && c4_build_target static32 c4core-coverage}
      - name: static32-coverage-artifacts
        uses: actions/upload-artifact@v3
        with:
          name: coverage-static32
          path: |
            build/static32/lcov/
            build/static32/coverage3-final_filtered.lcov
      - {name: static32-submit-codecov, run: source .github/setenv.sh && c4_submit_coverage static32 codecov}
      - {name: static32-submit-coveralls, run: source .github/setenv.sh && c4_submit_coverage static32 coveralls,
         env: {COVERALLS_FLAG_NAME: "${{matrix.name}}/static32"}}

  #----------------------------------------------------------------------------
  coverage_nofastfloat:
    name: coverage/${{matrix.name}}
    # if: github.ref == 'refs/heads/master'
    continue-on-error: true
    if: always()  # https://stackoverflow.com/questions/62045967/github-actions-is-there-a-way-to-continue-on-error-while-still-getting-correct
    runs-on: ${{matrix.os}}
    strategy:
      fail-fast: false
      matrix:
        include:
          - {name: nofastfloat/c++11, std: 11, cxx: g++-9, cc: gcc-9, bt: Coverage, os: ubuntu-20.04}
          - {name: nofastfloat/c++17, std: 17, cxx: g++-9, cc: gcc-9, bt: Coverage, os: ubuntu-20.04}
    env: {
      STD: "${{matrix.std}}",
      CXX_: "${{matrix.cxx}}",
      BT: "${{matrix.bt}}",
      OS: "${{matrix.os}}",
      CODECOV_TOKEN: "${{secrets.CODECOV_TOKEN}}",
      COVERALLS_REPO_TOKEN: "${{secrets.COVERALLS_REPO_TOKEN}}",
      COVERALLS_PARALLEL: true,   # https://docs.coveralls.io/parallel-build-webhook
      COVERALLS_FLAG_NAME: "${{matrix.name}}",
      BDIR:   "build/nofastfloat-${{matrix.cxx}}-cxx${{matrix.std}}",
      IDIR: "install/nofastfloat-${{matrix.cxx}}-cxx${{matrix.std}}",
    }
    steps:
      - {name: checkout, uses: actions/checkout@v3, with: {submodules: recursive}}
      - {name: install requirements, run: source .github/reqs.sh && c4_install_test_requirements $OS}
      - {name: show info, run: source .github/setenv.sh && c4_show_info}
      - name: nofastfloat-configure------------------------------------------------
        run: |
          set -x
          mkdir -p $BDIR
          mkdir -p $IDIR
          cmake -S . -B $BDIR \
            -DC4CORE_WITH_FASTFLOAT=OFF \
            -DC4_CXX_STANDARD=${{matrix.std}} \
            -DC4CORE_CXX_STANDARD=${{matrix.std}} \
            -DC4CORE_BUILD_TESTS=ON \
            -DC4CORE_VALGRIND=OFF \
            -DC4CORE_COVERAGE_CODECOV=ON \
            -DC4CORE_COVERAGE_COVERALLS=ON \
            -DCMAKE_INSTALL_PREFIX=$IDIR \
            -DCMAKE_BUILD_TYPE=Coverage \
            -DCMAKE_CXX_COMPILER=${{matrix.cxx}} \
            -DCMAKE_C_COMPILER=${{matrix.cc}}
      - {name: nofastfloat-build, run: cmake --build $BDIR --config Coverage --target c4core-test-build -j}
      - {name: nofastfloat-run, run: cmake --build $BDIR --config Coverage --target c4core-coverage}
      - name: nofastfloat-coverage-artifacts
        uses: actions/upload-artifact@v3
        with:
          name: coverage-shared32
          path: |
            build/nofastfloat-${{matrix.cxx}}-cxx${{matrix.std}}/lcov/
            build/nofastfloat-${{matrix.cxx}}-cxx${{matrix.std}}/coverage3-final_filtered.lcov
      - {name: nofastfloat-submit-codecov, run: cmake --build $BDIR --config Coverage --target c4core-coverage-submit-codecov}
      - {name: nofastfloat-submit-coveralls, run: cmake --build $BDIR --config Coverage --target c4core-coverage-submit-coveralls}

  # https://github.com/marketplace/actions/coveralls-github-action
  coveralls_finish:
    needs: [coverage, coverage_nofastfloat]
    runs-on: ubuntu-latest
    steps:
      - name: coveralls-notify
        continue-on-error: true
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.github_token }}
          parallel-finished: true
